package lol.clann.minecraft.springboot.api.bukkit.rawmessage;

import lol.clann.minecraft.springboot.api.bukkit.utils.ItemStackUtils;
import lol.clann.minecraft.springboot.api.context.SpringContext;
import lol.clann.minecraft.springboot.api.model.LazyField;
import org.bukkit.inventory.ItemStack;
import relocate.com.fasterxml.jackson.databind.node.ArrayNode;
import relocate.com.fasterxml.jackson.databind.node.JsonNodeFactory;

/**
 * @author pyz
 * @date 2018/8/18 上午11:04
 */
public class HoverEvent {

    private static final LazyField<ItemStackUtils> itemStackUtils = LazyField.of(() -> SpringContext.getBean(ItemStackUtils.class));

    /**
     * Shows text when you hover over it
     *
     * @param text The text to show
     * @return The {@link MessageEvent}
     */
    public static MessageEvent showText(String text) {
        return new MessageEvent("show_text", text);
    }

    /**
     * Shows text when you hover over it
     *
     * @param message The {@link RawMessage} to show
     * @return The {@link MessageEvent}
     */
    public static MessageEvent showText(RawMessage message) {
        ArrayNode arr = JsonNodeFactory.instance.arrayNode();
        arr.add(message.toJSON());
        return new MessageEvent("show_text", arr);
    }

    /**
     * Shows an achievement when you hover over it
     *
     * @param id The id of the achievement
     * @return The {@link MessageEvent}
     */
    public static MessageEvent showAchievement(String id) {
        return new MessageEvent("show_achievement", id);
    }

    public static MessageEvent showItemStack(ItemStack itemStack) {
        return new MessageEvent("show_item", itemStackUtils.get().toJsonStringForRawMessage(itemStack));
    }

}
